#include "apikeysdialog.h"
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QLabel>
#include <QMessageBox>
#include <QSettings>

ApiKeysDialog::ApiKeysDialog(QWidget *parent)
    : QDialog(parent)
{
    setWindowTitle("设置DeepSeek API密钥");
    setMinimumWidth(400);

    QVBoxLayout *mainLayout = new QVBoxLayout(this);
    
    // 说明标签
    QLabel *infoLabel = new QLabel("请输入DeepSeek API设置。您可以从DeepSeek官网获取API密钥和正确的API端点。", this);
    infoLabel->setWordWrap(true);
    mainLayout->addWidget(infoLabel);
    
    // API密钥输入框
    QLabel *apiKeyLabel = new QLabel("API密钥:", this);
    apiKeyInput = new QLineEdit(this);
    apiKeyInput->setEchoMode(QLineEdit::Password); // 隐藏输入内容
    apiKeyInput->setPlaceholderText("输入您的DeepSeek API密钥");
    
    QHBoxLayout *apiKeyLayout = new QHBoxLayout();
    apiKeyLayout->addWidget(apiKeyLabel);
    apiKeyLayout->addWidget(apiKeyInput);
    mainLayout->addLayout(apiKeyLayout);
    
    // API端点输入框
    QLabel *apiEndpointLabel = new QLabel("API端点:", this);
    apiEndpointInput = new QLineEdit(this);
    apiEndpointInput->setPlaceholderText("输入DeepSeek API端点URL");
    
    QHBoxLayout *apiEndpointLayout = new QHBoxLayout();
    apiEndpointLayout->addWidget(apiEndpointLabel);
    apiEndpointLayout->addWidget(apiEndpointInput);
    mainLayout->addLayout(apiEndpointLayout);
    
    // 按钮
    saveButton = new QPushButton("保存", this);
    cancelButton = new QPushButton("取消", this);
    
    QHBoxLayout *buttonLayout = new QHBoxLayout();
    buttonLayout->addStretch();
    buttonLayout->addWidget(saveButton);
    buttonLayout->addWidget(cancelButton);
    mainLayout->addLayout(buttonLayout);
    
    // 连接信号和槽
    connect(saveButton, &QPushButton::clicked, this, &ApiKeysDialog::saveApiKey);
    connect(cancelButton, &QPushButton::clicked, this, &QDialog::reject);
    
    // 加载已保存的设置
    loadSettings();
}

ApiKeysDialog::~ApiKeysDialog()
{
    // 清理资源
}

void ApiKeysDialog::loadSettings()
{
    QSettings settings("BMI8", "DeepSeekAPI");
    QString apiKey = settings.value("apiKey").toString();
    QString apiEndpoint = settings.value("apiEndpoint", "https://api.deepseek.com/v1/chat/completions").toString();
    
    if (!apiKey.isEmpty() && apiKey != "YOUR_DEEPSEEK_API_KEY") {
        apiKeyInput->setText(apiKey);
    }
    apiEndpointInput->setText(apiEndpoint);
}

void ApiKeysDialog::saveApiKey()
{
    QString apiKey = apiKeyInput->text().trimmed();
    QString apiEndpoint = apiEndpointInput->text().trimmed();
    
    if (apiKey.isEmpty()) {
        QMessageBox::warning(this, "错误", "API密钥不能为空");
        return;
    }
    
    if (apiEndpoint.isEmpty()) {
        QMessageBox::warning(this, "错误", "API端点不能为空");
        return;
    }
    
    // 保存API密钥和端点
    QSettings settings("BMI8", "DeepSeekAPI");
    settings.setValue("apiKey", apiKey);
    settings.setValue("apiEndpoint", apiEndpoint);
    
    QMessageBox::information(this, "成功", "API设置已保存");
    accept();
}